using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using Common;
using device;
using MainView.Windows;
using SExperiment;

namespace MainView
{
	public class DeviceFound
	{
		public static RS232Port GetDevice(string series)
		{
			List<RS232Port> source = (from s in Device.GetRS232PortInfo()
				where s.PortState == 0 || s.PortState == 1
				select s).ToList();
			RS232Port rS232Port = source.Where((RS232Port s) => s.DeviceNo == series).FirstOrDefault();
			if (rS232Port == null)
			{
				do
				{
					DeviceNotFoundWindow deviceNotFoundWindow = new DeviceNotFoundWindow();
					deviceNotFoundWindow.DisableVitrualMode();
					deviceNotFoundWindow.Owner = Utility.GetMainWindow();
					deviceNotFoundWindow.ShowDialog();
					if (deviceNotFoundWindow.DialogResult == DeviceNotFoundWindow.EDialogResult.Retry)
					{
						source = (from s in Device.GetRS232PortInfo()
							where s.PortState == 0 || s.PortState == 1
							select s).ToList();
						rS232Port = source.Where((RS232Port s) => s.DeviceNo == series).FirstOrDefault();
						continue;
					}
					return null;
				}
				while (rS232Port == null);
			}
			return rS232Port;
		}

		public static RS232Port GetDevice()
		{
			return GetDevice(false);
		}

		private static RS232Port GetDevice(bool useVirtual = true, ObservableCollection<Experiment> ExperimentList = null)
		{
			bool flag = false;
			List<RS232Port> list = (from s in Device.GetRS232PortInfo()
				where s.PortState == 0 || s.PortState == 1
				select s).ToList();
			if (list.Count == 0)
			{
				do
				{
					DeviceNotFoundWindow deviceNotFoundWindow = new DeviceNotFoundWindow();
					if (!useVirtual)
					{
						deviceNotFoundWindow.DisableVitrualMode();
					}
					deviceNotFoundWindow.Owner = Utility.GetMainWindow();
					deviceNotFoundWindow.ShowDialog();
					if (deviceNotFoundWindow.DialogResult == DeviceNotFoundWindow.EDialogResult.VirtualDevice)
					{
						flag = true;
						break;
					}
					if (deviceNotFoundWindow.DialogResult == DeviceNotFoundWindow.EDialogResult.Retry)
					{
						list = (from s in Device.GetRS232PortInfo()
							where s.PortState == 0 || s.PortState == 1
							select s).ToList();
						continue;
					}
					return null;
				}
				while (list.Count <= 0);
			}
			if (!flag)
			{
				list = list.Where((RS232Port s) => s.PortState == 0 || (s.DeviceModel != Device.DeviceModel_48P && s.DeviceModel != Device.DeviceModel_24P && s.PortState == 1 && (s.BlockAState == 0 || s.BlockBState == 0))).ToList();
				if (list.Count == 0)
				{
					MessageBox.Show(Utility.GetMessage("msg_cannot_create_experimetn_use_model_A_B"));
					return null;
				}
			}
			RS232Port result = null;
			if (list.Count == 1)
			{
				result = list[0];
			}
			else if (list.Count > 1)
			{
				DeviceSelectorWindow deviceSelectorWindow = new DeviceSelectorWindow();
				deviceSelectorWindow.ExperimentList = ExperimentList;
				deviceSelectorWindow.Ports = list;
				if (deviceSelectorWindow.ShowDialog() == true)
				{
					if (deviceSelectorWindow.SelectedIndex < 0 || deviceSelectorWindow.SelectedIndex >= list.Count)
					{
						return null;
					}
					result = list[deviceSelectorWindow.SelectedIndex];
				}
			}
			return result;
		}
	}
}
